package src.greedy_algorithms;

/**
 * @author starsea
 * @date 2024-08-29 8:53
 */

public class Test05 {
    public static void main(String[] args) {
        int[]  gas = {2,3,4};
        int[] cost = {3,4,3};
        int c=canCompleteCircuit(gas,cost);
    }
    public static int canCompleteCircuit(int[] gas, int[] cost) {
        int[] arr=new int[gas.length];
        for(int i=0;i<gas.length;i++)
        {
            arr[i]=gas[i]-cost[i];
        }
        for(int i=0;i<arr.length;i++)
        {
            int sum=0;
            int j=0;
            for(;j<arr.length;j++)
            {
                int post=(i+j)%arr.length;
                sum+=arr[post];
                if(sum<0)
                {
                    break;
                }
            }
            if(sum>=0)
            {
                return i;
            }
            i=i+j;

        }
        return -1;
    }
}
